import threading
import time
import datetime
import random
from bs4 import BeautifulSoup
from sendapi.BaseSend import *
class Reminder(threading.Thread):
    def __init__(self):
        super().__init__()
        self.setName('Reminder')
        self.open=False
        self.weekbegin=datetime.date(year=2021,month=3,day=1)
        self.group=258180904
        self.calssData=XD_remind().deal_data()
        self.keyWord=['帮助-课程提醒']
    def run(self):
        FriendApi(2384945985,1656301062).sendWord('课程提醒：已开启')
        while self.open:
            timeNow=time.time()
            weekBegin=self.weekbegin
            weekEnd=datetime.date.today()
            # 周次
            week=(weekEnd-weekBegin).days//7+1
            # 星期
            sunday = datetime.datetime.now().weekday() + 1
            # 小时:分钟
            hour=time.strftime('%H:%M',time.localtime(timeNow))
            for i in self.calssData:
                t,timeStr=self.dealTime(i['时间'])
                if week in i['周数'] and sunday==i['星期'] and t==hour:
                    strInf=self.getStr(i,timeStr)
                    GroupApi(bot=2384945985,group=self.group).sendWord(msg=strInf)
                    time.sleep(60)
            FriendApi(2384945985,1656301062).sendWord('课程提醒：iamhere')
            time.sleep(30)
        FriendApi(2384945985,1656301062).sendWord('课程提醒：已关闭')
    def hitokoto(self):
        data={"type":random.choice(['a','b','c','d','e','f'])}
        result=requests.post('https://v1.alapi.cn/api/hitokoto',data=data)
        dict=json.loads(result.text)
        return (dict['data']['hitokoto'],dict['data']['from'])
    def getStr(self,dict,timeStr):
        tip,source=self.hitokoto()
        str="上课提醒：\n" \
            "课程："+dict['班级']+"\n" \
            "时间："+timeStr+"\n" \
            "地点："+dict['地点']+"\n" \
            "Tip："+tip+"\n" \
            "-- "+source
        return str
    def dealTime(self,timeStr):
        timeoDict={1:'10:00',
                  2:'11:00',
                  3:'12:10',
                  4:'13:10',
                  5:'14:10',
                  6:'15:30',
                  7:'16:30',
                  8:'17:40',
                  9:'18:40',
                   10:'25:00',
                   11:'25:00',
                   12:'25:00',
                   13:'25:00',
                   14:'25:00'
                  }
        timeDict={1:'9:50',
                  2:'10:50',
                  3:'12:00',
                  4:'13:00',
                  5:'14:00',
                  6:'15:20',
                  7:'16:20',
                  8:'17:30',
                  9:'18:30',
                   10:'25:00',
                   11:'25:00',
                   12:'25:00',
                   13:'25:00',
                   14:'25:00'
                  }
        timefDict={1:'10:50',
                  2:'11:50',
                  3:'13:00',
                  4:'14:00',
                  5:'14:40',
                  6:'16:20',
                  7:'17:20',
                  8:'18:30',
                  9:'19:20',
                   10:'25:00',
                   11:'25:00',
                   12:'21:24',
                   13:'25:00',
                   14:'25:00'

                  }
        timeBegin=int(timeStr.split('-')[0])
        timeEnd=int(timeStr.split('-')[1])
        return (timeDict[timeBegin],timeoDict[timeBegin]+' - '+timefDict[timeEnd])
    def stop(self):
        self.open=False
class XD_remind():
    def __init__(self):
        self.Path='./program/data/classreminder/'
        # self.Path='./data/classreminder/'
    def search_data(self):
        pass
    def deal_data(self):
        classlisto=[]
        classlistf=[]
        weekdict={"一":1,"二":2,"三":3,"四":4,"五":5,"六":6,"七":7,}
        with open(self.Path+"class.txt",'r',encoding='utf-8') as f:
                str=f.read()
                soup=BeautifulSoup(str,'lxml')
                div_list=soup.find_all('tr')
        for i in div_list:
                list=i.find_all('td')
                dict={'班级':list[4].text,'周数':list[16].text,'时间':list[18].text,'教师':list[13].text,'地点':list[19].text}
                if dict not in classlisto:
                    classlisto.append(dict)
        classlisto.pop(0)
        for i in classlisto:
            zslist=[]
            l=i['周数'].split(',')
            for j in l:
                start=int(j.split('-')[0])
                end=int(j.split('-')[1])
                for k in range(start,end+1):
                    zslist.append(k)
            dict={'班级':i['班级'].split(']')[1],'周数':zslist,'星期':weekdict[i['时间'][0]],'时间':i['时间'][2:][:-2],'教师':i['教师'].split(']')[1],'地点':i['地点']}
            classlistf.append(dict)
        return classlistf

